package cn.hxzy.company.controller;

import cn.hxzy.company.entity.Member;
import cn.hxzy.company.entity.News;
import cn.hxzy.company.entity.User;
import cn.hxzy.company.entity.Work;
import cn.hxzy.company.entity.vo.PageBean;
import cn.hxzy.company.service.MemberService;
import cn.hxzy.company.service.NewsService;
import cn.hxzy.company.service.UserService;
import cn.hxzy.company.service.WorkService;
import cn.hxzy.company.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author little black
 * 后台管理页面
 */
@Controller
public class PublicController {
    @ModelAttribute
//*************完善后记得删了*****************
    //确定个初始值
    public void test(Map<String, Object> map) {
        User user = new User();
        News news = new News();
        Work work = new Work();
        user.setId(55);
        news.setUser(user);
        work.setUser(user);
        map.put("news", news);
    }
    //*****************

    @Autowired
    private UserService userService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private WorkService workService;

    /**
     * 首页面
     */
    @GetMapping("/admin/index")
    public ModelAndView adminIndex() {
        ModelAndView modelAndView = new ModelAndView("admin/index");
        return modelAndView;
    }

    /**
     * 用户管理
     */
//    添加用户
    @GetMapping("/admin/addUser")
    public ModelAndView addUser() {
        ModelAndView modelAndView = new ModelAndView("admin/addUser");
        return modelAndView;
    }

    //    代入数据->重定向到页面
    @PostMapping("/admin/saveUser")
    public String saveUser(User user) {
        String name = user.getName();
        System.out.println("name=" + name);
        String password = user.getPassword();
        System.out.println("password=" + password);
        String s = Md5Util.MD5(password);
        System.out.println(s);
        userService.insert(new User(name, s));
        return "redirect:/admin/showUser";
    }

    //    用户管理
    @GetMapping("/admin/showUser")
    public ModelAndView showUser(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size) {
        ModelAndView modelAndView = new ModelAndView("admin/showUser");
        PageBean pageBean = new PageBean<User>();
        pageBean.setPage(page);
        pageBean.setSize(size);
        userService.queryPage(pageBean);
        modelAndView.addObject("showUser", pageBean);
        return modelAndView;
    }

    //    模糊查询
    @PostMapping("/admin/showUser")
    public ModelAndView showUser(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size, String name, String id) {
        ModelAndView modelAndView = new ModelAndView("admin/showUser");
        if (id != null && id != "") {
            PageBean pageBean = new PageBean<User>();
            pageBean.setPage(page);
            pageBean.setSize(size);
            userService.queryPageWithId(pageBean, id);
            modelAndView.addObject("showUser", pageBean);
        }
        if (name != null && name != "") {
            PageBean pageBean = new PageBean<User>();
            pageBean.setPage(page);
            pageBean.setSize(size);
            userService.queryPageWithName(pageBean, name);
            modelAndView.addObject("showUser", pageBean);
        }
        return modelAndView;
    }

    //    删除用户
    @GetMapping("/admin/deleteUser/{id}")
    public String deleteUser(@PathVariable("id") Integer id) {
        userService.deleteById(id);
        return "redirect:/admin/showUser";
    }

    //修改用户
    @GetMapping("/admin/editUser/{id}")
    public ModelAndView editUser(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("admin/editUser");
        User users = userService.queryById(id);
        modelAndView.addObject("user", users);
        return modelAndView;
    }

    //    代入数据->重定向到页面
    @PostMapping("/admin/editUser")
    public String editUser(User user) {
        String s = Md5Util.MD5(user.getPassword());
        user.setPassword(s);
        userService.update(user);
        return "redirect:/admin/showUser";
    }

    /**
     * 新闻管理
     */
//    添加新闻
    @GetMapping("/admin/addNews")
    public ModelAndView addNews() {
        ModelAndView modelAndView = new ModelAndView("admin/addNews");
        return modelAndView;
    }


    //    代入数据->重定向到页面
    @PostMapping("/admin/saveNews")
    public String saveNews(News news) {
        news.setCreatTime(new Date());
        newsService.insert(news);
        return "redirect:/admin/showNews";
    }

    //    新闻管理
    @GetMapping("/admin/showNews")
    public ModelAndView showNews(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size) {
        ModelAndView modelAndView = new ModelAndView("admin/showNews");
        List<User> users = userService.queryAll();
        modelAndView.addObject("users", users);
        PageBean pageBean = new PageBean<News>();
        pageBean.setPage(page);
        pageBean.setSize(size);
        newsService.queryPage(pageBean);
        modelAndView.addObject("showNews", pageBean);
        return modelAndView;
    }

    //    模糊查询
    @PostMapping("/admin/showNews")
    public ModelAndView showNews(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size,
                                 String name, String id, String users, String date) {
        ModelAndView modelAndView = new ModelAndView("admin/showNews");
        List<User> users1 = userService.queryAll();
        modelAndView.addObject("users", users1);
        if (id != null && id != "") {
            PageBean pageBean = new PageBean<News>();
            pageBean.setPage(page);
            pageBean.setSize(size);
            newsService.queryPageWithId(pageBean, id);
            modelAndView.addObject("showNews", pageBean);
        }
        if (users != null && users != "") {
            PageBean pageBean = new PageBean<News>();
            pageBean.setPage(page);
            pageBean.setSize(size);
            newsService.queryPageWithUsers(pageBean, users);
            modelAndView.addObject("showNews", pageBean);
        }
        if (name != null && name != "") {
            PageBean pageBean = new PageBean<News>();
            pageBean.setPage(page);
            pageBean.setSize(size);
            newsService.queryPageWithName(pageBean, name);
            modelAndView.addObject("showNews", pageBean);
        }
        if (date != null && date != "") {
            PageBean pageBean = new PageBean<News>();
            pageBean.setPage(page);
            pageBean.setSize(size);
            newsService.queryPageWithDate(pageBean, date);
            modelAndView.addObject("showNews", pageBean);
        }
        return modelAndView;
    }

    //    删除新闻
    @GetMapping("/admin/deleteNews/{id}")
    public String deleteNews(@PathVariable("id") Integer id) {
        newsService.deleteById(id);
        return "redirect:/admin/showNews";
    }

    //修改用户
    @GetMapping("/admin/editNews/{id}")
    public ModelAndView editNews(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("admin/editNews");
        News news = newsService.queryById(id);
        modelAndView.addObject("news", news);
        return modelAndView;
    }

    //    代入数据->重定向到页面
    @PostMapping("/admin/editNews")
    public String editNews(News news) {
        newsService.update(news);
        return "redirect:/admin/showNews";
    }

    /**
     * 作品管理
     */
//    添加作品
    @GetMapping("/admin/addWork")
    public ModelAndView addWork() {
        ModelAndView modelAndView = new ModelAndView("admin/addWork");
        return modelAndView;
    }

    //    代入数据->重定向到页面
    @PostMapping("/admin/saveWork")
    public String saveWork(Work work, MultipartFile file, HttpSession session) throws IOException {
        String filename = file.getOriginalFilename();
        String type = filename.indexOf(".") != -1 ? filename.substring(filename.lastIndexOf(".")) : null;
        String fileName = UUID.randomUUID().toString().replaceAll("-", "");

        File file1 = new File("G:/resource/zy-company/img/work/" + fileName + type);
        file.transferTo(file1);
        work.setImage("/img/work/" + fileName + type);
        User user=new User();
        user.setId(1);
        work.setUser(user);
        work.setCreatTime(new Date());
        workService.insert(work);
        return "redirect:/admin/showWork";

    }

    //    作品管理
    @GetMapping("/admin/showWork")
    public ModelAndView showWork(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size) {
        ModelAndView modelAndView = new ModelAndView("admin/showWork");
//        返显发布人
        List<User> users = userService.queryAll();
        modelAndView.addObject("users", users);
//        内容显示
        PageBean pageBean = new PageBean<Work>();
        pageBean.setPage(page);
        pageBean.setSize(size);
        workService.queryPage(pageBean);
        modelAndView.addObject("showWork", pageBean);
        return modelAndView;
    }

    //    模糊查询
    @PostMapping("/admin/showWork")
    public ModelAndView showWork(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size,
                                 String name, String id, String users, String date) {
        ModelAndView modelAndView = new ModelAndView("admin/showWork");
//        发布人返现
        List<User> users1 = userService.queryAll();
        modelAndView.addObject("users", users1);
//        模糊查询
        if (id != null && id != "") {
            PageBean pageBean = new PageBean<Work>();
            pageBean.setPage(page);
            pageBean.setSize(size);
            workService.queryPageWithId(pageBean, id);
            modelAndView.addObject("showWork", pageBean);
        }
        if (users != null && users != "") {
            PageBean pageBean = new PageBean<Work>();
            pageBean.setPage(page);
            pageBean.setSize(size);
            workService.queryPageWithUsers(pageBean, users);
            modelAndView.addObject("showWork", pageBean);
        }
        if (name != null && name != "") {
            PageBean pageBean = new PageBean<Work>();
            pageBean.setPage(page);
            pageBean.setSize(size);
            workService.queryPageWithName(pageBean, name);
            modelAndView.addObject("showWork", pageBean);
        }
        if (date != null && date != "") {
            System.out.println(date);
            PageBean pageBean = new PageBean<Work>();
            pageBean.setPage(page);
            pageBean.setSize(size);
            workService.queryPageWithDate(pageBean, date);
            modelAndView.addObject("showWork", pageBean);
        }
        return modelAndView;
    }

    //    删除作品
    @GetMapping("/admin/deleteWork/{id}")
    public String deleteWork(@PathVariable("id") Integer id) {
        workService.deleteById(id);
        return "redirect:/admin/showWork";
    }

    //修改作品
    @GetMapping("/admin/editWork/{id}")
    public ModelAndView editWork(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("admin/editWork");
        Work work = workService.queryById(id);
        modelAndView.addObject("work", work);
        return modelAndView;
    }

    //    代入数据->重定向到页面
    @PostMapping("/admin/editWork")
    public String editWork(Work work, MultipartFile file, HttpSession session) throws IOException {
        String filename = file.getOriginalFilename();
        String type = filename.indexOf(".") != -1 ? filename.substring(filename.lastIndexOf(".")) : null;
        String fileName = UUID.randomUUID().toString().replaceAll("-", "");

        File file1 = new File("G:/resource/zy-company/img/work/" + fileName + type);
        file.transferTo(file1);
        work.setImage("/img/work/" + fileName + type);
        User user = (User) session.getAttribute("loginUser");
        System.out.println(user);
        work.setUser(user);
        work.setCreatTime(work.getCreatTime());
        workService.update(work);
        return "redirect:/admin/showWork";
    }

    /**
     * 公司成员管理
     *
     * @return
     */
    //添加成员
    @GetMapping("/admin/addMember")
    public ModelAndView addMember() {
        ModelAndView modelAndView = new ModelAndView("admin/addMember");
        return modelAndView;
    }

    //    代入数据->重定向到页面
    @PostMapping("/admin/saveMember")
    public String saveMember(Member member, MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String type = filename.indexOf(".") != -1 ? filename.substring(filename.lastIndexOf(".")) : null;
        String fileName = UUID.randomUUID().toString().replaceAll("-", "");

        File file1 = new File("G:/IDEA/zy_company/src/main/webapp/img/member/" + fileName + type);
        file.transferTo(file1);
        member.setImage("/img/member/" + fileName + type);
        System.out.println(member);
        memberService.insert(member);
        return "redirect:/admin/showMember";
    }

    //成员管理
    @GetMapping("/admin/showMember")
    public ModelAndView showMember(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size) {
        ModelAndView modelAndView = new ModelAndView("admin/showMember");
        PageBean pageBean = new PageBean<Member>();
        pageBean.setPage(page);
        pageBean.setSize(size);
        memberService.queryPage(pageBean);
        modelAndView.addObject("showMember", pageBean);
        return modelAndView;
    }

    //    模糊查询
    @PostMapping("/admin/showMember")
    public ModelAndView showMember(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "6") Integer size,
                                   String name, String id, String memberWork) {
        ModelAndView modelAndView = new ModelAndView("admin/showMember");
//        模糊查询
        if (id != null && id != "") {
            PageBean pageBean = new PageBean<Member>();
            pageBean.setPage(page);
            pageBean.setSize(size);
            memberService.queryPageWithId(pageBean, id);
            modelAndView.addObject("showMember", pageBean);
        }
        if (name != null && name != "") {
            PageBean pageBean = new PageBean<Member>();
            pageBean.setPage(page);
            pageBean.setSize(size);
            memberService.queryPageWithName(pageBean, name);
            modelAndView.addObject("showMember", pageBean);
        }
        if (memberWork != null && memberWork != "") {
            System.out.println(memberWork);
            PageBean pageBean = new PageBean<Member>();
            pageBean.setPage(page);
            pageBean.setSize(size);
            memberService.queryPageWithMemberWork(pageBean, memberWork);
            modelAndView.addObject("showMember", pageBean);
        }
        return modelAndView;
    }


    //    删除成员
    @GetMapping("/admin/deleteMember/{id}")
    public String deleteMember(@PathVariable("id") Integer id) {
        memberService.deleteById(id);
        return "redirect:/admin/showMember";
    }

    //修改成员
    @GetMapping("/admin/editMember/{id}")
    public ModelAndView editMember(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("admin/editMember");
        Member member = memberService.queryById(id);
        modelAndView.addObject("member", member);
        return modelAndView;
    }

    //    代入数据->重定向到页面
    @PostMapping("/admin/editMember")
    public String editMember(Member member, MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String type = filename.indexOf(".") != -1 ? filename.substring(filename.lastIndexOf(".")) : null;
        String fileName = UUID.randomUUID().toString().replaceAll("-", "");

        File file1 = new File("G:/IDEA/zy_company/src/main/webapp/img/member/" + fileName + type);
        file.transferTo(file1);
        member.setImage("/img/member/" + fileName + type);
        memberService.update(member);
        return "redirect:/admin/showMember";
    }
}
